package logic

import (
	"apple/applerpc/applerpc"
	"apple/applerpc/internal/svc"
	"apple/cache"
	"apple/models/mysql"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type PasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PasswordLogic {
	return &PasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 重置密码
func (l *PasswordLogic) Password(in *applerpc.PasswordRequest) (*applerpc.PasswordResponse, error) {
	// todo: add your logic here and delete this line
	//var user mysql.User
	//mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	//if user.ID == 0 {
	//	return nil, fmt.Errorf("不存在")
	//}
	//if in.Password != in.Newpassword {
	//	return nil, fmt.Errorf("新密码和确认不一致")
	//}
	//if in.Newpassword == user.Password {
	//	return nil, fmt.Errorf("新密码不能喝旧密码一致")
	//}
	//get := cache.Rdb.Get(l.ctx, "sendsms"+in.Tel)
	//if get.Val() != in.SendSms {
	//	return nil, fmt.Errorf("短信错误")
	//}
	//user.Password = in.Newpassword
	//mysql.DB.Debug().Updates(&user)
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account)
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}
	if in.Password != in.Newpassword {
		return nil, fmt.Errorf("确认密码与第一次不一致")
	}
	if in.Newpassword == user.Password {
		return nil, fmt.Errorf("新密码与旧密码不能一致")
	}
	get := cache.Rdb.Get(l.ctx, "sendsms"+in.Tel)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信错误")
	}
	user.Password = in.Newpassword
	return &applerpc.PasswordResponse{
		Id: int64(user.ID),
	}, nil
}
